package com.xiongjie.health;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.healthchecks.HealthCheckHandler;
import io.vertx.ext.healthchecks.HealthChecks;
import io.vertx.ext.healthchecks.Status;
import io.vertx.ext.web.Router;

public class EventBusProducerHealthChecks extends AbstractVerticle {

    @Override
    public void start() {
        vertx.eventBus().<String>consumer("hello", message -> {
            message.reply(new JsonObject());
        });

        //检查eventbus是否在监听某个地址，比如rest的地址
        HealthCheckHandler healthCheckHandler2 = HealthCheckHandler.createWithHealthChecks(HealthChecks.create(vertx));
        healthCheckHandler2.register("ebHealthCheck", future ->
                vertx.eventBus().request("hello", "ping", response -> {
                    if (response.succeeded()) {
                        future.complete(Status.OK());
                    } else {
                        future.complete(Status.KO());
                    }
                })
        );
        Router router = Router.router(vertx);
        router.get("/").handler(healthCheckHandler2);
        vertx.createHttpServer()
                .requestHandler(router)
                .listen(8080);
    }
}
